#include "gui_util.h"
#include "type.h"



void gui_util_check_init() {
    int flags = IMG_INIT_PNG | IMG_INIT_JPG;
    int initted = IMG_Init(flags);

    if ((initted & flags) != flags) {
        printf("IMG_Init: Failed to init required png AND JPG support!\n");
        printf("IMG_Init: %s\n", IMG_GetError());
        exit(1);
    }
}

SDL_Surface* gui_util_load_png_img(const char* file) {
    SDL_Surface* locky_debug = NULL;
    SDL_RWops *locky_rwop;
    locky_rwop = SDL_RWFromFile(file, "rb");
    locky_debug = IMG_LoadPNG_RW(locky_rwop);
    if (!locky_debug) {
        printf("IMG_LoadPNG_RW: %s\n", IMG_GetError());
        /* handle error*/
    }
    SDL_RWclose(locky_rwop);
    return locky_debug;
}

SDL_Surface *gui_util_drawtext(TTF_Font *fonttodraw, short fgR, short fgG, short fgB, short fgA, short bgR, short bgG, short bgB, short bgA, char text[]) {
    SDL_Color tmpfontcolor = {fgR, fgG, fgB, fgA};
    //TODO migliorare rendering e verificare prestazioni */
    // FIXME migliorare rendering e verificare prestazioni
    /* @todo ciao */
    // SDL_Color tmpfontbgcolor = {bgR, bgG, bgB, bgA};
    SDL_Surface *resulting_text;
    resulting_text = TTF_RenderText_Solid(fonttodraw, text, tmpfontcolor);
    //resulting_text = TTF_RenderText_Shaded(fonttodraw, text, tmpfontcolor, tmpfontbgcolor);
    //resulting_text = TTF_RenderText_Blended(fonttodraw, text, tmpfontcolor);
    return resulting_text;
}

char* gui_util_convert_key_to_char(main_obj *mobj, SDLKey sk) {
    char *c = SDL_GetKeyName(sk);
    gpointer p = g_hash_table_lookup(mobj->sm->gt, c);
    if (p == NULL) {
        return c;
    } else {
        return p;
    }
}

gboolean gui_util_element_is_clicked(main_obj *mobj, graphic_element *ge) {
    if ((mobj->event->button.x > ge->posx) && (mobj->event->button.x < (ge->posx + ge->graphic->w))) {
        if ((mobj->event->button.y > ge->posy) && (mobj->event->button.y < (ge->posy + ge->graphic->h))) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
}

SDL_Surface* gui_util_get_new_empty_surface(int w, int h) {
    Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif
    SDL_Surface *s = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_ALPHA_TRANSPARENT, w, h, 32, rmask, gmask, bmask, amask);
    return s;
}

/**
 * imposta la posizione di un elemento grafico
 * @param ge
 * @param x
 * @param y
 */
void gui_util_set_element_pos(graphic_element *ge, int x, int y) {
    ge->posx = x;
    ge->posy = y;
}

/**
 * TODO da completare i vari case
 * @param mobj
 * @param ge
 * @param gp
 */
void gui_util_set_element_pos_flag(main_obj *mobj, graphic_element *ge, int gp) {
    switch (gp) {
        case (GUI_POS_CENTER_H):
        {
            ge->posx = (mobj->main_screen->w / 2) - (ge->graphic->w / 2);
            break;
        }
        case (GUI_POS_CENTER_V):
        {
            ge->posy = (mobj->main_screen->h / 2) - (ge->graphic->h / 2);
            break;
        }
    }
}

void gui_util_set_position_on_screen(main_obj *mobj, graphic_element *ge, int position) {
    switch (position) {
        case (GUI_POSITION_LEFT):
        {
            ge->posx = 0;
            break;
        }
        case (GUI_POSITION_HMIDDLE):
        {
            ge->posx = (mobj->s->vs->screen_width / 2) - (ge->graphic->w / 2);
            break;
        }
        case (GUI_POSITION_RIGHT):
        {
            ge->posx = mobj->s->vs->screen_width - ge->graphic->w;
            break;
        }
        case (GUI_POSITION_TOP):
        {
            ge->posy = 0;
            break;
        }
        case (GUI_POSITION_VMIDDLE):
        {
            ge->posy = (mobj->s->vs->screen_height / 2) - (ge->graphic->h / 2);
            break;
        }
        case (GUI_POSITION_BOTTOM):
        {
            ge->posy = mobj->s->vs->screen_height - ge->graphic->h;
            break;
        }
    }
}

/**
 * posizione centralle x
 * @param mobj
 * @return 
 */
gint gui_util_get_center_x(main_obj *mobj)
{
    return mobj->main_screen->w / 2;
}

/**
 * posizione centrale y
 * @param mobj
 * @return 
 */
gint gui_util_get_center_y(main_obj *mobj)
{
    return mobj->main_screen->h / 2;
}